package com.example.smartwaterapi.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.smartwaterapi.entity.DailyStats;
import com.example.smartwaterapi.mapper.DailyStatsMapper;
import com.example.smartwaterapi.service.DailyStatsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * 每日统计服务实现类
 */
@Slf4j
@Service
public class DailyStatsServiceImpl extends ServiceImpl<DailyStatsMapper, DailyStats> implements DailyStatsService {

    // Implement custom methods here if defined in the interface
    
    @Override
    public boolean save(DailyStats entity) {
        log.info("保存每日统计数据: {}", entity);
        try {
            boolean result = super.save(entity);
            if (result) {
                log.info("每日统计数据保存成功, id: {}", entity.getId());
            } else {
                log.error("每日统计数据保存失败");
            }
            return result;
        } catch (Exception e) {
            log.error("保存每日统计数据异常", e);
            throw e;
        }
    }
    
    @Override
    public boolean updateById(DailyStats entity) {
        log.info("更新每日统计数据, id: {}", entity.getId());
        try {
            boolean result = super.updateById(entity);
            if (result) {
                log.info("每日统计数据更新成功, id: {}", entity.getId());
            } else {
                log.warn("每日统计数据更新失败, id: {}, 可能记录不存在", entity.getId());
            }
            return result;
        } catch (Exception e) {
            log.error("更新每日统计数据异常, id: {}", entity.getId(), e);
            throw e;
        }
    }

    public DailyStats getById(Long id) {
        log.info("获取每日统计数据, id: {}", id);
        try {
            DailyStats stats = super.getById(id);
            if (stats == null) {
                log.warn("未找到每日统计数据, id: {}", id);
            }
            return stats;
        } catch (Exception e) {
            log.error("获取每日统计数据异常, id: {}", id, e);
            throw e;
        }
    }

    public boolean removeById(Long id) {
        log.info("删除每日统计数据, id: {}", id);
        try {
            boolean result = super.removeById(id);
            if (result) {
                log.info("每日统计数据删除成功, id: {}", id);
            } else {
                log.warn("每日统计数据删除失败, id: {}, 可能记录不存在", id);
            }
            return result;
        } catch (Exception e) {
            log.error("删除每日统计数据异常, id: {}", id, e);
            throw e;
        }
    }
}
